package com.quzhi.sys.controller.UserController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.Result;
import com.quzhi.sys.entity.RoleMenu;
import com.quzhi.sys.service.impl.RoleMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Xibing
 * @since 2024-08-18
 */
@RestController
@RequestMapping("/sys/roleMenu")
public class RoleMenuController {


    @Autowired
    private RoleMenuServiceImpl roleMenuService;

    /**
     * 保存用户所有的路由
     */
    @PostMapping("/{id}")
    public Result<?> saveRoleMenu(@PathVariable String id, @RequestBody List<String> menuIds) {
        // 删除该用户所有的路由设置
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoleMenu::getRoleId, id);
        roleMenuService.remove(lambdaQueryWrapper);

        // 保存现在的传入进来的路由
        for (String menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setId(UUID.randomUUID().toString());
            roleMenu.setRoleId(id);
            roleMenu.setMenuId(menuId);

            roleMenuService.save(roleMenu);
        }

        return Result.success("配置用户路由成功");
    }


    /**
     * 获取用户的所有路由
     */
    @GetMapping("/{id}")
    public Result<?> getRoleMenu(@PathVariable String id) {
        // 获取制定角色的路由
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId, id);

        List<RoleMenu> list = roleMenuService.list(roleMenuLambdaQueryWrapper);


        return Result.success(list);

    }


}
